/**
 * 
 */
package com.huawei.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.huawei.struts.form.ChannelForm;
import com.huawei.struts.service.IChannelService;
import com.huawei.struts.util.PageBean;

/**
 * @author huganpu
 *
 */
public class ChannelAction extends BaseAction {

	private static Logger logger=Logger.getLogger(ChannelAction.class);
	
	private final String returnUrl="/channel.do?method=init";
	
	private IChannelService ChannelService;
	

	public void setChannelService(IChannelService channelService) {
		ChannelService = channelService;
	}

	/**
	 * 初始化用户列表
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward init(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ChannelForm ChannelForm =(ChannelForm)form;
		Integer parentId=new Integer(request.getParameter("p_id"));
		logger.debug("into Channel init method ...");
		List list=ChannelService.getALlChannelByParentId(parentId);
		request.setAttribute("list", list);
		request.setAttribute("p_id", parentId);
		return mapping.findForward("toList");
	}
	
	public ActionForward search(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ChannelForm ChannelForm =(ChannelForm)form;
		logger.debug("into Channel search method ...");
		PageBean pageBean=ChannelService.getChannelList(ChannelForm.getChannel(), ChannelForm.getPageBean());
		request.setAttribute("pageBean", pageBean);
		return mapping.findForward("toList");
	}
	
	/**
	 * 跳转到用户创建页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toCreateChannel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ChannelForm ChannelForm =(ChannelForm)form;
		logger.debug("into Channel toCreateChannel method ...");
		this.saveToken(request);
		return mapping.findForward("toAdd");
	}
	
	/**
	 * 创建用户
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward createChannel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ChannelForm ChannelForm =(ChannelForm)form;
		logger.debug("into Channel createChannel method ...");
		if(this.isTokenValid(request,true))
		{
			ChannelService.addChannel(ChannelForm.getChannel());
			return super.goSuccess("操作成功", "/channel.do?method=init&p_id="+ChannelForm.getChannel().getParentId(), request, response, mapping);
		}
		else
		{
			return super.goFail("表单不能重复提交", "/channel.do?method=init&p_id="+ChannelForm.getChannel().getParentId(), request, response,mapping);
		}
	}
	
	/**
	 * 跳转到用户修改页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toUpdateChannel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ChannelForm ChannelForm =(ChannelForm)form;
		logger.debug("into Channel toUpdateChannel method ...");
		Integer id=new Integer(request.getParameter("id"));
		ChannelForm.setChannel(ChannelService.getChannelInfoByid(id));
		this.saveToken(request);
		return mapping.findForward("toUpdate");
	}
	
	/**
	 * 修改用户
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward updateChannel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ChannelForm ChannelForm =(ChannelForm)form;
		logger.debug("into Channel updateChannel method ...");
		if(this.isTokenValid(request,true))
		{
			ChannelService.modifyChannel(ChannelForm.getChannel());
			return super.goSuccess("操作成功", "/channel.do?method=init&p_id="+ChannelForm.getChannel().getParentId(), request, response, mapping);
		}
		else
		{
			return super.goFail("表单不能重复提交", "/channel.do?method=init&p_id="+ChannelForm.getChannel().getParentId(), request, response,mapping);
		}
	}
	
	public ActionForward delChannel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ChannelForm ChannelForm =(ChannelForm)form;
		Integer id=new Integer(request.getParameter("id"));
		ChannelForm.getChannel().setChannelId(id);
		ChannelService.delChannel(ChannelForm.getChannel());
		return super.goSuccess("操作成功", "/channel.do?method=init&p_id="+ChannelForm.getChannel().getParentId(), request, response, mapping); 
	}
	
	public ActionForward selectchannel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ChannelForm ChannelForm =(ChannelForm)form;
		Integer parentId=new Integer(request.getParameter("p_id"));
		logger.debug("into Channel init method ...");
		List list=ChannelService.getALlChannelByParentId(parentId);
		request.setAttribute("list", list);
		return mapping.findForward("toSelectchannel");
	}
	

}
